﻿@using DevExtreme.AspNet.Data.ResponseModel
@using DevExtreme.AspNet.Data
@using System.Threading
@using System.Text.Json
<DemoPageSectionComponent Id="Editors-TagBox-EmptyDataAreaTemplate" ShowSizeMode="true">
    <ChildContentWithParameters Context="Params">
        @inject NwindDataService NwindDataService

        <DxTagBox TData="@WebApiLookup" TValue="string"
                    CustomData="@LoadCustomData"
                    SizeMode="Params.SizeMode"
                    ListRenderMode="ListRenderMode.Virtual"
                    CssClass="cw-480"
                    NullText="Select a value..."
                    DropDownVisibleChanged="@OnDropDownVisibleChanged"
                    ClearButtonDisplayMode="@DataEditorClearButtonDisplayMode.Auto"
                    InputId="tbEmptyDataAreaTemplate">
            <EmptyDataAreaTemplate>
                @if(context.IsDataLoading) {
                    <div class="empty-data-area-template">
                        <div class="d-flex flex-column">
                            <DxWaitIndicator Visible="true"
                                             SizeMode="Params.SizeMode"
                                             CssClass="m-auto"
                                             AnimationType="WaitIndicatorAnimationType.Spin" />
                            <p class="dxbl-text d-block mt-1">Loading, please wait...</p>
                        </div>
                    </div>
                }
            </EmptyDataAreaTemplate>
        </DxTagBox>
    </ChildContentWithParameters>

    @code {
        [Inject] protected HttpClient Client { get; set; }
        bool ThrottleRequireTotalCount { get; set; } = true;

        async Task<LoadResult> LoadCustomData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken) {
            using var response = await Client.GetAsync(options.ConvertToGetRequestUri("https://js.devexpress.com/Demos/NetCore/api/DataGridWebApi/CustomersLookup"), cancellationToken);
            response.EnsureSuccessStatusCode();
            while(options.RequireTotalCount && ThrottleRequireTotalCount) {
                cancellationToken.ThrowIfCancellationRequested();
                await Task.Yield();
            }
            using var responseStream = await response.Content.ReadAsStreamAsync();
            var result = await JsonSerializer.DeserializeAsync<LoadResult>(responseStream, cancellationToken: cancellationToken);
            return result;
        }

        void OnDropDownVisibleChanged(bool value) {
            if(!value) {
                ThrottleRequireTotalCount = true;
            } else {
                _ = ResetThrottleRequireTotalCountAsync(true);
            }
        }

        async Task ResetThrottleRequireTotalCountAsync(bool value) {
            try {
                await Task.Delay(TimeSpan.FromSeconds(5));
                ThrottleRequireTotalCount = !value;
            }
            catch(Exception ex) {
            }
        }

        public class WebApiLookup {
            public string Text { get; set; }
            public string Value { get; set; }
        }
    }
</DemoPageSectionComponent>
